﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using System.Collections;

namespace Common.DataBase {
    public class TableTools<T> where T : new()
    {

        /// <summary>
        /// DataTable转换成Object
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public T GetObjectFromTable(DataTable dataTable)
        {
            List<T> list = GetListFromTable(dataTable);
            return (list == null || list.Count <= 0) ? default(T) : list[0];
        }

        /// <summary>
        /// DataTable转换成List
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<T> GetListFromTable(DataTable dataTable)
        {
            List<T> list = new List<T>();
            if (dataTable == null || dataTable.Rows.Count == 0)
            {
                return null;
            }
            PropertyInfo[] properties = typeof(T).GetProperties();//获取实体类型的属性集合
            List<string> columnNameList = GetColumnNames(dataTable.Columns);//按照属性顺序的列名集合           
            T model = default(T);
            foreach (DataRow dataRow in dataTable.Rows)
            {
                model = new T();//创建实体
                foreach (PropertyInfo property in properties)
                {
                    foreach(DataColumn column in dataTable.Columns)
                    {
                        if (column.ColumnName.ToLower().Trim() == property.Name.ToLower().Trim())
                        {
                            object value = dataRow[column.ColumnName];
                            if (value != DBNull.Value && property.CanWrite)
                            {
                                property.SetValue(model, value, null);
                            }
                        }
                    }
                    
                   
                }
                list.Add(model);
            }
            return list;
        } 


        /// <summary>
        /// 按照属性顺序的列名集合
        /// </summary>
        public static List<string> GetColumnNames(DataColumnCollection dataColumnCollection)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();//获取实体类型的属性集合
            List<string> list = new List<string>();
            foreach (PropertyInfo property in properties)
            {
                foreach (DataColumn dataColumn in dataColumnCollection)
                {
                    if (dataColumn.ColumnName.ToLower().Contains(property.Name.ToLower()))
                    {
                        list.Add(dataColumn.ColumnName);
                    }
                }
            }
            return list;
        }



        /// <summary>
        /// DataTable转换成List<Dictionary<string,string>>
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public  List<Dictionary<string,string>> GetListDictionaryFromTable(DataTable dataTable)
        {
            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            if (dataTable == null || dataTable.Rows.Count == 0)
            {
                return list;
            }
            foreach (DataRow dataRow in dataTable.Rows) {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                foreach (DataColumn dataColumn in dataTable.Columns) { 
                    object value = dataRow[dataColumn.ColumnName];
                    dictionary[dataColumn.ColumnName] = value == DBNull.Value ? "" : value.ToString();
                } 
                list.Add(dictionary);
            }
            return list;
        } 


    }

}
